package main

import (
	"BlogPro/dao"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func main() {

	db, err := gorm.Open(mysql.Open("root:6235290@tcp(localhost:3306)/gorm?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	sqlDB, _ := db.DB()
	defer sqlDB.Close()
	//err=db.AutoMigrate(&dao.Product{})//自动创建表
	//m := db.Migrator() //手动创建 {CreateTable,DropTable,HasTable,RenameTable,GetTables()}
	//err = m.CreateTable(&User{})
	//if err != nil {
	//	fmt.Println(err)
	//}
	//res:=db.Create(&dao.Product{
	//	Id:    3,
	//	Name:  "iphone 13 proMax",
	//	Price: 9999,
	//	Pic:   "pic1.png",
	//})
	//if  res.RowsAffected<=0{
	//	println("插入失败")
	//}else {
	//	printf("插入了%d条数据",res.RowsAffected)
	//}
	//查询
	//user:=dao.User{}
	//db.First(&user)

	//fmt.Println(user)
	//var users []dao.User
	//db.Find(&users)
	//for _,v:=range users{
	//	fmt.Println(v)
	//}
	//var users []dao.User
	//db.First(&users,203)
	////// SELECT * FROM users WHERE id = 203;
	//for _,v:=range users{
	//	fmt.Println(v)
	//}
	//db.Order("id desc").Limit(10).Find(&users)
	//for _,v:=range users{
	//	fmt.Println(v)
	//}
	//for i:=0;i<=100;i++ {
	//	user:=dao.User{
	//		//Id:   i,
	//		Name: "xt"+strconv.Itoa(i),
	//		Age:  20+i%10,
	//		Addr: "浙江省杭州市",
	//		Pic:  "p"+strconv.Itoa(i)+".jpg",}
	//	sqldb:=db.Create(&user)
	//	fmt.Print(sqldb)
	//}

	//update
	//db.Model(&dao.User{}).Where("Id",404).Update("name","xt666")
	////updates
	//db.Model(&dao.User{}).Where("Id",404).Updates(dao.User{
	//	Name: "xt777",
	//	Age:  23,
	//	Addr: "111",
	//	Pic:  "1111",
	//})
	//delete
	res := db.Delete(&dao.User{}, 401)
	//res:=db.Where("Name","xt98").Delete(&dao.User{})
	fmt.Println(res.RowsAffected)
}
